package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.ReOrder;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.service.IReOrderService;
import com.baomidou.ant.service.ISysUserService;
import com.baomidou.ant.vo.request.RequestAddReProVo;
import com.baomidou.ant.vo.request.RequestCheckReOrderVo;
import com.baomidou.ant.vo.request.RequestCheckStatusVo;
import com.baomidou.ant.vo.request.RequestListReOrderVo;
import com.baomidou.ant.vo.response.ResponseListReOrderVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/re-order")
public class ReOrderController {
    @Autowired
    IReOrderService reOrderService;
    @Autowired
    ISysUserService userService;

    @PostMapping("/addReOrder")
    public Result addReOrder(@RequestBody RequestAddReProVo vo){
        String string=reOrderService.addReOrder(vo);
        if(Objects.equals(string, "EXIST_NOT_CHECK")){
            return Result.getFailure().setMsg("提交失败，该lot存在未审核的返工单!");
        }else if(Objects.equals(string, "OLD_LOT_ERROR")){
            return Result.getFailure().setMsg("提交失败，错误的lot号!");
        }else if(Objects.equals(string, "NOT_START")){
            return Result.getFailure().setMsg("工单未开始！无法返工！");
        }else if(Objects.equals(string, "SUCCESS")){
            return Result.getSuccess().setMsg("保存成功，请等待审核处理！");
        }else
            return Result.getFailure().setMsg("发生错误，保存失败，请重试!");
    }

    @PostMapping("/downStatus")
    public Result downStatus(@RequestBody RequestCheckStatusVo vo){
        //判断单据状态
        ReOrder reOrder=reOrderService.queryByReLot(vo.getReLot());
        if(Objects.equals(reOrder.getStatus(), "2")){
            return Result.getFailure().setMsg("作废单据无法列印！");
        }else if(Objects.equals(reOrder.getStatus(), "0")){
            return Result.getFailure().setMsg("单据未审核，无法列印！");
        }else {
            if(reOrder.getIsDown()){
                return Result.getFailure().setMsg("单据已列印，无法重复操作！<br/>列印人为："+reOrder.getDownWorker());
            }else
                return Result.getSuccess();
        }

    }

    @PostMapping("/checkStatus")
    public Result checkStatus(@RequestBody RequestCheckStatusVo vo){
        //先判断用户权限
        SysUser user=userService.queryByLoginName(vo.getLoginName());
        if(Objects.equals(user.getType(), "2")){
            return Result.getFailure().setMsg("权限不足！");
        }
        //判断单据状态
        ReOrder reOrder=reOrderService.queryByReLot(vo.getReLot());
        if(ObjectUtils.isEmpty(reOrder)){
            return Result.getFailure().setMsg("未找到该返工单！");
        }else if(Objects.equals(reOrder.getStatus(), "2")){
            return Result.getFailure().setMsg("该返工单已审核，结果：作废！");
        }else if(Objects.equals(reOrder.getStatus(), "1")){
            return Result.getFailure().setMsg("该返工单已审核通过,无需重复操作！");
        }else
            return Result.getSuccess();
    }

    @PostMapping("/checkIt")
    public Result checkIt(@RequestBody RequestCheckReOrderVo vo){
        ReOrder reOrder=reOrderService.queryByReLot(vo.getReLot());
        if(ObjectUtils.isEmpty(reOrder)){
            return Result.getFailure().setMsg("提交失败,未知工单!");
        }
        if(Objects.equals(vo.getStatus(), "1")){
            reOrder.setStatus(vo.getStatus());
        }else
            reOrder.setStatus("2");
        if(reOrderService.updateById(reOrder)){
            return Result.getSuccess().setMsg("提交成功！");
        }else
            return Result.getFailure().setMsg("提交失败！保存出错");

    }

    @PostMapping("/listReOrder")
    public Result listReOrder(@RequestBody RequestListReOrderVo vo){
        IPage<ResponseListReOrderVo> iPage=reOrderService.reOrderList(vo);
        return Result.getSuccess().setData(iPage);
    }

}
